﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 路线推荐
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015-8-3 11:40
    public class CBizRoute : CustBizObject
    {
        private CDmDjkRoute m_CDmDjkRoute = new CDmDjkRoute();
        private CDmDjkRoute oDmDjkRoute
        {
            get { return m_CDmDjkRoute; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizRoute()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkRoute.DJK_ROUTE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ROUTE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }
        #region
        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }
        #endregion

        /// <summary>
        /// 添加 路线推荐
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="introduction">简介</param>
        /// <param name="characteristic">特色</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="city">市</param>
        /// <param name="area">区</param>
        /// <param name="scenicSpots">涵盖景点</param>
        /// <param name="coverageArea">覆盖地区</param>
        /// <param name="imageExplain">路线首图</param>
        /// <param name="routeCountent">路线介绍</param>
        /// <param name="price">价格</param>
        /// <param name="isDjkInfo">是否有推送的地接客</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 12:05
        public string[] AddRoute(string title, string introduction, string characteristic, string country, string province, string area, string scenicSpots, string coverageArea, string imageExplain, string routeCountent, string price, int isDjkInfo)
        {

            if (title.Length > 30)
            {
                return new string[] { "false", "标题应该在30字以内" };
            }
            if (introduction.Length > 100)
            {
                return new string[] { "false", "简介应该在100字以内" };
            }
            string routeid = this.DataObj.NewID();
            DataSet dsRoute = new CDmDjkRoute();
            DataRow drRoute = dsRoute.Tables[0].NewRow();
            drRoute["ROUTE_ID"] = routeid;

            drRoute["ROUTE_TITEL"] = title;

            drRoute["ROUTE_CHARACTERISTIC"] = characteristic;
            drRoute["ROUTE_COUNTRY"] = country;
            drRoute["ROUTE_PROVINCE"] = province;
            drRoute["ROUTE_AREA"] = area;
            drRoute["COVERAGE_SCENIC_SPOTS"] = scenicSpots;
            drRoute["COVERAGE_AREA"] = coverageArea;
            drRoute["IMAGE_EXPLAIN"] = imageExplain;

            drRoute["ROUTE_PRIC"] = price;
            drRoute["IS_DJKINFO"] = isDjkInfo;

            drRoute["CREATE_TIME"] = this.dtCurrDateTime;
            drRoute["IS_DELETE"] = 0;
            drRoute["INTRODUCTION"] = introduction;
            //路线介绍
            string filePath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV;
            string strVpath = filePath + this.DataObj.NewID() + ".html";//存储文件的虚拟路径
            string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);

            using (System.IO.TextWriter streamWriter = new System.IO.StreamWriter(strFFullPath))
            {
                streamWriter.Write(routeCountent);
            }
            drRoute["ROUTE_INTRODUCTION"] = strVpath;


            dsRoute.Tables[0].Rows.Add(drRoute);
            dsRoute.Tables[0].TableName = this.TableName;
            this.Update(dsRoute);
            return new string[] { "true", routeid };
        }

        /// <summary>
        /// 修改 路线推荐
        /// </summary>
        /// <param name="routeid">修改的路线ID</param>
        /// <param name="title">标题</param>
        /// <param name="introduction">简介</param>
        /// <param name="characteristic">特色</param>
        /// <param name="country">国家</param>
        /// <param name="province">省</param>
        /// <param name="city">市</param>
        /// <param name="area">区</param>
        /// <param name="scenicSpots">涵盖景点</param>
        /// <param name="coverageArea">覆盖地区</param>
        /// <param name="imageExplain">路线首图</param>
        /// <param name="routeCountent">路线介绍</param>
        /// <param name="price">价格</param>
        /// <param name="isDjkInfo">是否有推送的地接客</param>
        /// <returns>string</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 12:05
        public string[] UpdateRoute(string routeid,string title, string introduction, string characteristic, string country, string province, string area, string scenicSpots, string coverageArea, string imageExplain, string routeCountent, string price, int isDjkInfo)
        {
            if (title.Length > 30)
            {
                return new string[] { "false", "标题应该在30字以内" };
            }
            if (introduction.Length > 100)
            {
                return new string[] { "false", "简介应该在100字以内" };
            }

            DataSet ds = GetRouteInfoByID(routeid);
            DataTable dt = ds.Tables[0];
            if (dt==null)
            {
                return new string[] { "false", "未找到该路线信息" };
            }
            
            dt.Rows[0]["ROUTE_ID"] = routeid;

            dt.Rows[0]["ROUTE_TITEL"] = title;

            dt.Rows[0]["ROUTE_CHARACTERISTIC"] = characteristic;
            dt.Rows[0]["ROUTE_COUNTRY"] = country;
            dt.Rows[0]["ROUTE_PROVINCE"] = province;
            dt.Rows[0]["ROUTE_AREA"] = area;
            dt.Rows[0]["COVERAGE_SCENIC_SPOTS"] = scenicSpots;
            dt.Rows[0]["COVERAGE_AREA"] = coverageArea;
            dt.Rows[0]["IMAGE_EXPLAIN"] = imageExplain;

            dt.Rows[0]["ROUTE_PRIC"] = price;
            dt.Rows[0]["IS_DJKINFO"] = isDjkInfo;

            dt.Rows[0]["CREATE_TIME"] = this.dtCurrDateTime;
            dt.Rows[0]["IS_DELETE"] = 0;
            dt.Rows[0]["INTRODUCTION"] = introduction;
            //路线介绍
            
            //存储文件的虚拟路径
            string strVpath = dt.Rows[0]["ROUTE_INTRODUCTION"].ToString();
            string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
            using (System.IO.TextWriter streamWriter = new System.IO.StreamWriter(strFFullPath))
            {
                streamWriter.Write(routeCountent);
            }
            //dt.Rows[0]["ROUTE_INTRODUCTION"] = strVpath;

            this.Update(ds);
            return new string[] { "true", routeid };
        }

        /// <summary>
        /// 获取路线 列表信息
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="introduction">简介</param>
        /// <param name="scenicSpots">涵盖景点</param>
        /// <param name="routecountry">国家</param>
        /// <param name="province">城市</param>
        /// <param name="area">目的地</param>
        /// <param name="pagesize">每页多少数据</param>
        /// <param name="pageindex">第几页</param>
        /// <returns>DataTable</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 15:44
        public DataTable GetRouteList(string key, string routecountry, string province, string area, int pagesize, int pageindex)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("key", ParameterDirection.Input, EDbType.String, 20, key));

            parmCollection.Add(new CDbParm("routecountry", ParameterDirection.Input, EDbType.String, 36, routecountry));

            parmCollection.Add(new CDbParm("province", ParameterDirection.Input, EDbType.String, 36, province));

            parmCollection.Add(new CDbParm("area", ParameterDirection.Input, EDbType.String, 36, area));

            parmCollection.Add(new CDbParm("pagesize", ParameterDirection.Input, EDbType.Integer, 10, pagesize));
            parmCollection.Add(new CDbParm("pageindex", ParameterDirection.Input, EDbType.Integer, 10, pageindex));

            DataSet ds = this.DataObj.QueryBySP("DJK_ROUTE_LIST_VIEW", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }

            return ds.Tables[0];
        }

        /// <summary>
        /// 根据 线路查询推荐 的地接客信息
        /// </summary>
        /// <param name="routeID">推荐线路iD</param>
        /// <returns></returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-3 14:37
        public DataTable GetRouteInfo(string routeID)
        {
            if (string.IsNullOrEmpty(routeID))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeID", ParameterDirection.Input, EDbType.String, 36, routeID));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT R.[ROUTE_ID],[ROUTE_TITEL],[ROUTE_INTRODUCTION],DC.CUSTOMER_REGISTER_ID,DC.CUSTOMER_NICK,DC.CUSTOMER_DEFAULT_PIC, 
R.ROUTE_COUNTRY,R.ROUTE_PROVINCE,R.ROUTE_AREA,
CC.[COUNTRY_CITY_NME] countyName,CC1.[COUNTRY_CITY_NME] provinceName,CC2.[COUNTRY_CITY_NME] areaName
FROM [dbo].[DJK_ROUTE](NOLOCK) R
LEFT JOIN [dbo].[DJK_COUNTRY_CITY](NOLOCK) CC
ON CC.[COUNTRY_CITY_ID]=R.ROUTE_COUNTRY
LEFT JOIN [dbo].[DJK_COUNTRY_CITY](NOLOCK) CC1
ON CC1.[COUNTRY_CITY_ID]=R.ROUTE_PROVINCE
LEFT JOIN [dbo].[DJK_COUNTRY_CITY](NOLOCK) CC2
ON CC2.[COUNTRY_CITY_ID]=R.ROUTE_AREA
LEFT JOIN [dbo].[DJK_ADDITIONAL_INFO](NOLOCK) A
ON R.[ROUTE_ID]=A.ROUTE_ID
LEFT JOIN [dbo].[DJK_CUSTOMER_REGISTER](NOLOCK) DC
ON A.DJK_USERID=DC.CUSTOMER_REGISTER_ID 
WHERE R.[ROUTE_ID]=@routeID", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            return ds.Tables[0];
        }

        /// <summary>
        /// 获取路线信息
        /// </summary>
        /// <param name="routeID">路线ID</param>
        /// <returns>DataTable</returns>
        /// Author:蒲崇彬
        /// Creation date:2015-8-6 9:43
        public DataSet GetRouteInfoByID(string routeID)
        {
            if (string.IsNullOrEmpty(routeID))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("routeID", ParameterDirection.Input, EDbType.String, 36, routeID));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_ROUTE WHERE ROUTE_ID=@routeID", parmCollection);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }

    }
}
